﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Blackbird.Core.Editable;

namespace Blackbird.Editors.Controls
{
    /// <summary>
    /// Interaction logic for FrameStripImage.xaml
    /// </summary>
    public partial class FrameStripItem : UserControl
    {
        public static readonly RoutedEvent SelectionChangedEvent = EventManager.RegisterRoutedEvent("SelectionChanged", RoutingStrategy.Bubble,
            typeof(RoutedEventHandler), typeof(FrameStripItem));

        public event RoutedEventHandler SelectionChanged
        {
            add { AddHandler(SelectionChangedEvent, value); }
            remove { RemoveHandler(SelectionChangedEvent, value); }
        }

        #region Dependency Properties

        private EditableFrame frame;
        public EditableFrame Frame
        {
            get { return frame; }
            set { frame = value; FrameImage.Source = value.BitmapSource; }
        }

        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set
            {
                if (IsSelected != value)
                {
                    if (value)
                    {
                        FrameCanvas.Style = (Style)Resources["FrameCanvasSelectedStyle"];
                        GrabberTagBorder.Style = (Style)Resources["FrameCanvasSelectedStyle"];
                    }
                    else
                    {
                        FrameCanvas.Style = (Style)Resources["FrameCanvasUnselectedStyle"];
                        GrabberTagBorder.Style = (Style)Resources["FrameCanvasUnselectedStyle"];                    
                    }

                    SetValue(IsSelectedProperty, value);
                    RaiseSelectionChangedEvent();
                }
            }
        }

        // Using a DependencyProperty as the backing store for IsSelected.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(FrameStripItem), new UIPropertyMetadata(false));

        #endregion


        public bool IsSaved { get; set; }

        public FrameStripItem()
        {
            InitializeComponent();
        }

        private void RaiseSelectionChangedEvent()
        {
            RoutedEventArgs args = new RoutedEventArgs(FrameStripItem.SelectionChangedEvent, this);
            RaiseEvent(args);
        }
    }
}
